//
//  HMRetweetView.swift
//  HMWeiBo
//
//  Created by Apple on 16/8/1.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class HMRetweetView: UIView {

    var constraint: Constraint?
    
    var retweetViewModel: HMHomeCellViewModel?{
    
        didSet{
            
            
            // 赋值
//            contentLabel.text = retweetViewModel?.statusModel?.retweeted_status?.text
    
            contentLabel.attributedText = retweetViewModel?.dealEmoticon(retweetViewModel?.statusModel?.retweeted_status?.text)
            
            constraint?.uninstall()
            
            print(retweetViewModel?.statusModel?.retweeted_status?.pic_urls)
            
            // 
            if let pic_urls = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_urls.count > 0{
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    // 2层意思
                    // 1. 给 控件设置约束 make.bottom.equalTo(pictureView.snp_bottom)
                    // 2.  make.bottom.equalTo(pictureView.snp_bottom).constraint 有一个返回值,我们去记录这个返回值,用于下次卸载
                    self.constraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                pictureView.pic_urls = pic_urls
                
            } else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            }
            
        }
        
    }
    
    // 第一步: 重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步: 定义一个添加子类和设置子类控件约束的方法
    private func setupUI(){
        
        backgroundColor = UIColor.yellowColor()
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            make.width.equalTo(100)
            make.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
          self.constraint =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    //第三步: 懒加载控件
    //MARK:-- 懒加载控件
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "转发微博"
        label.numberOfLines = 0
        
        return label
    }()
    
    private lazy var pictureView: HMPhotoView = HMPhotoView()
}
